﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL.Interfaces;
using BAL.Interfaces;
using DAL;
using DAL.Infrastructure;
using Microsoft.Practices.Unity;

namespace BAL.Infrastructure
{
    public abstract class ServiceBase<TEntity> : IService<TEntity> where TEntity : class, IEntity
    {
        protected static IApplicationContext _context = null; 
        protected IRepository<TEntity> _repository = null; 

        public ServiceBase(IRepository<TEntity> repository, IApplicationContext context)
        {
            _context = context;
            _repository = repository;
        }

        public IRepository<TEntity> repository 
        {
            get { return _repository; }
            set { _repository = value; }
        }

        public IApplicationContext context
        {
            get { return _context; }
            set { _context = value; }
        }

        public void Create(TEntity entity)
        {
            try
            {
                _repository.Add(entity);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void Edit(TEntity entity)
        {
            try
            {
                _repository.Edit(entity);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void Remove(TEntity entity)
        {
            try
            {
                _repository.Remove(entity);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<TEntity> GetAll()
        {
            try
            {
                return _repository.GetAll();
            }catch(Exception ex)
            {
                throw ex;
            }
        }
        
        public TEntity Find(object[] keyValues)
        {
            try
            {
                return _repository.Find(keyValues);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public abstract IQueryable<TEntity> GetAllValidados();
    }
}
